package org.sasha.roulette.feed;

import java.util.Arrays;

import javax.inject.Inject;

import org.sasha.roulette.data.RouletteConstant;

import com.github.peholmst.mvp4vaadin.AbstractView;
import com.vaadin.data.Container.Indexed;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.CellStyleGenerator;
import com.vaadin.ui.Table.ColumnGenerator;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ChameleonTheme;

@SuppressWarnings("serial")
public class ColorFeedViewImpl extends AbstractView<ColorFeedView, ColorFeedPresenter> implements ColorFeedView {
    
    private VerticalLayout wrapper = new VerticalLayout();
    
    private Table table = new Table();

    @Inject
    public ColorFeedViewImpl(final ColorFeedPresenter presenter) {
        super(false);
        table.setSizeFull();
        wrapper.addStyleName("feed-wrapper");
        wrapper.setSizeFull();
        wrapper.addComponent(table);
        presenter.setView(this);
        setPresenter(presenter);
        init();
    }
    
    @Override
    public void init() {
        super.init();
        table.addGeneratedColumn("red", new ColumnGenerator() {
            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                Integer value = (Integer)source.getItem(itemId).getItemProperty("VALUE").getValue();
                return (value != null && Arrays.binarySearch(RouletteConstant.RED_VAL, value) >= 0) ? value : "";
            }
        });
        table.addGeneratedColumn("black", new ColumnGenerator() {
            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                Integer value = (Integer)source.getItem(itemId).getItemProperty("VALUE").getValue();
                return (value != null && Arrays.binarySearch(RouletteConstant.BLACK_VAL, value) >= 0) ? value : ""; 
            }
        });
        table.addGeneratedColumn("zero", new ColumnGenerator() {
            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                Integer value = (Integer)source.getItem(itemId).getItemProperty("VALUE").getValue();
                return (value != null && value == 0) ? 0 : "";
            }
        });
        
        table.setCellStyleGenerator(new CellStyleGenerator() {
            @Override
            public String getStyle(Object itemId, Object propertyId) {
                if ("red".equals(propertyId)) {
                    return "red";
                }
                if ("black".equals(propertyId)) {
                    return "black";
                }
                if ("zero".equals(propertyId)) {
                    return "zero";
                }
                return null;
            }
        });
    }
    
    @Override
    public ComponentContainer getViewComponent() {
        return wrapper;
    }
    
    @Override
    public void setFeedData(final Indexed data) {
        table.setContainerDataSource(data);
        table.setSizeFull();
        table.addStyleName(ChameleonTheme.TABLE_BORDERLESS);
        table.addStyleName("headless");
        table.setVisibleColumns(new Object[]{"red", "black", "zero"});
    }

    @Override
    public void refresh() {
        getPresenter().reloadData();
    }
}
